package com.zcx.qshop.action;

import android.content.Context;
import android.content.Intent;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.activity.ConfirmOrderActivity;
import com.zcx.qshop.activity.LoginActivity;
import com.zcx.qshop.conn.JsonAttributeAsyGet;
import com.zcx.qshop.conn.JsonGoodsattributeAsyGet;
import com.zcx.qshop.conn.JsonOrdersubmitAsyGet;
import com.zcx.qshop.conn.JsonShopcartAsyGet;
import com.zcx.qshop.conn.JsonShopcartaddAsyGet;
import com.zcx.qshop.dialog.CommodityAttributeDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/11/20.
 */
public class ShoppingAction {

    private OnShopCarChangeCallBack onShopCarChangeCallBack;

    private List<String> pids = new ArrayList<>();

    private List<JsonShopcartAsyGet.Info.Shop> shops = new ArrayList<>();

    private Context context;

    private String pid;

    private CommodityAttributeDialog attributeDialog;

    private JsonShopcartAsyGet jsonShopcartAsyGet = new JsonShopcartAsyGet("", "", new AsyCallBack<JsonShopcartAsyGet.Info>(){

        @Override
        public void onSuccess(String toast, int type, JsonShopcartAsyGet.Info info) throws Exception {

            pids.clear();

            pids.addAll(info.pids);

            shops.clear();

            shops.addAll(info.shops);

            if(onShopCarChangeCallBack != null){

                onShopCarChangeCallBack.onShopCarChange();

            }

        }

    });

    public void showAttribute(final Context context, final String pid){

        dismissAttribute();

        try{

            if(attributeDialog != null && this.context == context && this.pid != null && this.pid.equals(pid)){

                attributeDialog.show();

            }else{

                new JsonGoodsattributeAsyGet(this.pid = pid, new AsyCallBack<JsonGoodsattributeAsyGet.Info>(){

                    protected String gait, number;

                    @Override
                    public void onSuccess(String toast, int type, JsonGoodsattributeAsyGet.Info attribut){

                        attributeDialog = new CommodityAttributeDialog(ShoppingAction.this.context = context, attribut, pid, "1"){

                            @Override
                            public void onAttribute(boolean isInfo, JsonAttributeAsyGet.Info info) {

                                gait = info.gaid;

                            }

                            @Override
                            public void onCountChange(String count) {

                                number = count;

                            }

                            @Override
                            public void onAddShopCarClick() {

                                if(gait != null && !gait.equals("")){

                                    addShopCar(context, pid, number, gait);

                                }

                            }

                            @Override
                            public void onBuyClick() {

                                if(gait != null && !gait.equals("")){

                                    purchase(context, pid, number, gait);

                                }

                            }

                        };

                        attributeDialog.show();

                    }

                }).execute(context);

            }

        }catch (Exception e){}

    }

    public void dismissAttribute() {

        try {

            if(this.attributeDialog != null) {

                attributeDialog.dismiss();

            }

        } catch (Exception e) {}

    }

    public void addShopCar(final Context context, String pid, String number, String gait){

        if(QSApplication.LoginAction.isLogin()){

            if(gait == null || gait.equals("")){

                UtilToast.show(context, "请选择商品属性");

            }else{

                new JsonShopcartaddAsyGet(QSApplication.QSPreferences.readUid(), pid, number, gait , QSApplication.QSPreferences.readCity().code, new AsyCallBack(){

                    @Override
                    public void onSuccess(String toast, int type, Object o) throws Exception {

                        UtilToast.show(context, "加入购物车成功");

                        requestData(context);

                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {

                        UtilToast.show(context, "加入购物车失败");

                    }

                }).execute(context);

            }

        }else{

            context.startActivity(new Intent(context, LoginActivity.class));

        }

    }

    public void purchase(final Context context, String pid, String number, String gait){

        if(QSApplication.LoginAction.isLogin()){

            if(gait == null || gait.equals("")){

                UtilToast.show(context, "请选择商品属性");

            }else{

                new JsonOrdersubmitAsyGet(QSApplication.QSPreferences.readUid(), "now", "", pid, gait, number, new AsyCallBack<JsonOrdersubmitAsyGet.Info>(){

                    @Override
                    public void onSuccess(String toast, int type, JsonOrdersubmitAsyGet.Info info) throws Exception {

                        context.startActivity(new Intent(context, ConfirmOrderActivity.class).putExtra("Info", info));

                    }

                    @Override
                    public void onEnd(String toast, int type) throws Exception {

                        UtilToast.show(context, toast);

                    }

                }).execute(context);

            }

        }else{

            context.startActivity(new Intent(context, LoginActivity.class));

        }

    }

    public void settle(final Context context){

        String cid = "";

        for (int i = 0 ; i < shops.size() ; i ++){

            JsonShopcartAsyGet.Info.Shop shop = shops.get(i);

            if(shop.isSelect){

                cid += shop.cid + "," ;

            }

        }

        if(cid.contains(",")){

            cid = cid.substring(0, cid.length() - 1);

        }

        new JsonOrdersubmitAsyGet(QSApplication.QSPreferences.readUid(), "shopcar", cid, "", "", "", new AsyCallBack<JsonOrdersubmitAsyGet.Info>(){

            @Override
            public void onSuccess(String toast, int type, JsonOrdersubmitAsyGet.Info info) throws Exception {

                context.startActivity(new Intent(context, ConfirmOrderActivity.class).putExtra("Info", info));

            }

        }).execute(context);

    }

    public void requestData(Context context){

        jsonShopcartAsyGet.uid = QSApplication.QSPreferences.readUid();

        jsonShopcartAsyGet.areaid = QSApplication.QSPreferences.readCity().code;

        jsonShopcartAsyGet.execute(context);

    }

    public void removeData(int position){

        pids.remove(position);

        shops.remove(position);

        if(onShopCarChangeCallBack != null){

            onShopCarChangeCallBack.onShopCarChange();

        }

    }

    public void clearData(){

        pids.clear();

        shops.clear();

        if(onShopCarChangeCallBack != null){

            onShopCarChangeCallBack.onShopCarChange();

        }

    }

    public void setOnShopCarChangeCallBack(OnShopCarChangeCallBack onShopCarChangeCallBack){

        this.onShopCarChangeCallBack = onShopCarChangeCallBack;

        this.onShopCarChangeCallBack.onShopCarBack(shops);

    }

    public interface OnShopCarChangeCallBack{

        void onShopCarBack(List<JsonShopcartAsyGet.Info.Shop> shops);

        void onShopCarChange();

    }

}
